﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLNS_BLL;
using QLNS_Public;

namespace QLNS_GiaoDien
{
    public partial class uc_ChamCong : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_ChamCong()
        {
            InitializeComponent();
        }
        ChamCong_BLL chamcong_bll = new ChamCong_BLL();
        ChamCong_Public cc = new ChamCong_Public();
        private void uc_ChamCong_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = chamcong_bll.LayDuLieu();
        }
        int flag;
        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = 0;
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = 1;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            cc.MaCong = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Chấm Công"));
            cc.MaNhanVien = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhân Viên"));
            if (XtraMessageBox.Show("Bạn có muốn xóa Mã chấm công: '" + cc.MaCong + "' của nhân viên có Mã: '" + cc.MaNhanVien + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                chamcong_bll.Delete_ChamCong(cc);
                uc_ChamCong_Load(sender,e);
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                cc.MaNhanVien = int.Parse(txt_MaNV.Text);
                cc.Thang = int.Parse(cob_Thang.Text);
                cc.Nam = int.Parse(txt_Nam.Text);
                cc.NgayNghiCoPhep = int.Parse(txt_NgayNghiCoPhep.Text);
                cc.NgayNghiKhongPhep = int.Parse(txt_NgayNghiKhongPhep.Text);
                chamcong_bll.Insert_ChamCong(cc,cc,cc,cc,cc);
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (flag == 1)
            {
                cc.MaCong = int.Parse(txt_MaCong.Text);
                cc.MaNhanVien = int.Parse(txt_MaNV.Text);
                cc.Thang = int.Parse(cob_Thang.Text);
                cc.Nam = int.Parse(txt_Nam.Text);
                cc.NgayNghiCoPhep = int.Parse(txt_NgayNghiCoPhep.Text);
                cc.NgayNghiKhongPhep = int.Parse(txt_NgayNghiKhongPhep.Text);
                chamcong_bll.Update_ChamCong(cc,cc, cc, cc, cc, cc);
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            uc_ChamCong_Load(sender,e);
        }

        private void txt_MaNV_TextChanged(object sender, EventArgs e)
        {
            string sql = "select tbl_NhanVien.HoTen,tbl_PhongBan.TenPhong from tbl_NhanVien,tbl_PhongBan where tbl_NhanVien.MaPhong=tbl_PhongBan.MaPhong and tbl_NhanVien.MaNhanVien='"+txt_MaNV.Text+"'";
            DataTable dt = new DataTable();
            dt = chamcong_bll.taobang(sql);
            cob_HoTen.DataSource = dt;
            cob_HoTen.DisplayMember = "HoTen";
            cob_PhongBan.DataSource = dt;
            cob_PhongBan.DisplayMember = "TenPhong";
        }

        private void txt_NgayNghiCoPhep_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_NgayNghiKhongPhep_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaCong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void cob_Thang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }
    }
}
